package at.jku.se.fastsell.rest;

import java.util.logging.Logger;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import at.jku.se.fastsell.data.DataAccess;

@Path("/users")
public class UserResource {

	private static final Logger LOG = Logger.getLogger(UserResource.class.getName());

	// ----------------------------------------------------------------------------------

	@GET
	@Path("/{username}/verify")
	@Produces("text/plain")
	public Response placeBid(@PathParam("username") String username, @QueryParam("password") String password) {
		String response = "";
		// --
		try {
			if (DataAccess.verifyUser(username, password)) {
				response = "OK";
			} else {
				response = "Wrong credentials for " + username;
				LOG.warning(response);
			}
		} catch (Exception e) {
			response = e.toString();
			LOG.warning(response);
		}
		// --
		return RestHelper.buildResponse(response);
	}

	// ----------------------------------------------------------------------------------

	@POST
	@Path("/create")
	@Produces("text/plain")
	public Response createUser(@QueryParam("username") String username,
			@QueryParam("password") String password) {
		String response = "";
		// --
		try {
			DataAccess.createUser(username, password);
			LOG.info("User '" + username + "' successfully created");
			response = "OK";
		} catch (Exception e) {
			response = "Could not create user: " + e.toString();
			LOG.warning(response);
		}
		// --
		return RestHelper.buildResponse(response);
	}
}
